package com.bm.specialvehiclefordispatcher.accountsecurity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

import com.android.volley.VolleyError;
import com.bm.corelibs.AppManager;
import com.bm.corelibs.logic.BaseLogic.NListener;
import com.bm.corelibs.utils.ToastMgr;
import com.bm.specialvehiclefordispatcher.R;
import com.bm.specialvehiclefordispatcher.Tools.Tools;
import com.bm.specialvehiclefordispatcher.activity.BaseActivity;
import com.bm.specialvehiclefordispatcher.activity.login.LoginActivity;
import com.bm.specialvehiclefordispatcher.bean.BaseData;
import com.bm.specialvehiclefordispatcher.constants.Constant;
import com.bm.specialvehiclefordispatcher.logics.LoginManager;
import com.bm.specialvehiclefordispatcher.views.CommonNavigationBar;

public class UpdatePasswordActivity extends BaseActivity {
	CommonNavigationBar bar;
	EditText updatepsw_old;
	EditText updatepsw_new;
	EditText updatepsw_renew;
	Button updatepsw_sure;
	LoginManager manager = new LoginManager();
	

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_updatepassword);
		findViews();
		init();
		addListeners();
	}

	@Override
	public void findViews() {
		bar = (CommonNavigationBar) findViewById(R.id.navi);
		bar.setListener(null);
		bar.setTitle("修改登录密码");
		updatepsw_old = (EditText) findViewById(R.id.updatepsw_old);
		updatepsw_new = (EditText) findViewById(R.id.updatepsw_new);
		updatepsw_renew = (EditText) findViewById(R.id.updatepsw_renew);
		updatepsw_sure = (Button) findViewById(R.id.updatepsw_sure);

	}

	@Override
	public void init() {

	}
	
	private NListener<BaseData> resetListener() {
		return new NListener<BaseData>() {
			@Override
			public void onResponse(BaseData data) {
				loadingDialog.dismiss();
				if (data.status == Constant.STATUS_OK) {
					ToastMgr.show("修改成功, 请重新登录");
					LoginManager.getInstance(mActivity).clearUserInfo();
					AppManager.getAppManager().finishAllActivity();
					
					startActivity(new Intent(UpdatePasswordActivity.this, LoginActivity.class));
				} else {
					ToastMgr.show(data.msg);
				}
			}

			@Override
			public void onErrResponse(VolleyError error) {
				loadingDialog.dismiss();
			}

			@Override
			public void onAllPageLoaded(int nowPage, int totalPage) {
			}
		};
	}

	@Override
	public void addListeners() {
		updatepsw_sure.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (Tools.getTextViewText(updatepsw_old).length() >= 6) {

					if (Tools.getTextViewText(updatepsw_new).length() >= 6
							&& Tools.getTextViewText(updatepsw_renew)
									.length() >= 6) {
						if (Tools.getTextViewText(updatepsw_new).equals(
								Tools.getTextViewText(updatepsw_renew))) {
							loadingDialog.show();
							manager.resetPassword(LoginManager.getInstance(mActivity).getId(), Tools.getTextViewText(updatepsw_old), Tools.getTextViewText(updatepsw_new), resetListener());
						} else {
							showToast("两次新密码不一致");
						}

					} else {
						showToast("请输入6-16位新密码");
					}

				} else {
					showToast("原密码为6-16位");
				}
				

			}
		});
	}

}
